package com.yike.user.controller;

import com.yike.common.api.ApiResult;
import com.yike.common.enums.OperationLogType;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.vo.LoginUserInfoVo;
import com.yike.user.param.AgentBindPhoneParam;
import com.yike.user.param.AgentBindWxParam;
import com.yike.user.param.GetAppCodeParam;
import com.yike.user.param.LoginParam;
import com.yike.user.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Sakura
 * @date 2024/8/20 10:21
 */
@Slf4j
@RestController
@Module("user")
@Api(value = "登录管理API", tags = {"登录管理"})
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    @OperationLog(name = "统一登录", type = OperationLogType.LOGIN)
    @ApiOperation(value = "统一登录", response = LoginUserInfoVo.class)
    public ApiResult<LoginUserInfoVo> login(@Validated @RequestBody LoginParam loginParam) throws Exception {
        LoginUserInfoVo loginUserInfoVo = loginService.login(loginParam);
        return ApiResult.ok(loginUserInfoVo);
    }

    @PostMapping("/agentBindPhone")
    @OperationLog(name = "代理商绑定手机号", type = OperationLogType.LOGIN)
    @ApiOperation(value = "代理商绑定手机号", response = LoginUserInfoVo.class)
    public ApiResult<LoginUserInfoVo> agentBindPhone(@Validated @RequestBody AgentBindPhoneParam agentBindPhoneParam) throws Exception {
        LoginUserInfoVo loginUserInfoVo = loginService.agentBindPhone(agentBindPhoneParam);
        return ApiResult.ok(loginUserInfoVo);
    }

    @PostMapping("/agentBindWx")
    @OperationLog(name = "代理商绑定微信", type = OperationLogType.LOGIN)
    @ApiOperation(value = "代理商绑定微信", response = ApiResult.class)
    public ApiResult<String> agentBindWx(@Validated @RequestBody AgentBindWxParam agentBindWxParam) throws Exception {
        String wxName = loginService.agentBindWx(agentBindWxParam);
        return ApiResult.ok(wxName);
    }

    @GetMapping("/getWxPhone/{code}")
    @OperationLog(name = "获取微信授权手机号", type = OperationLogType.INFO)
    @ApiOperation(value = "获取微信授权手机号", response = ApiResult.class)
    public ApiResult<Boolean> getWxPhone(@PathVariable String code) throws Exception {
        boolean flag = loginService.getWxPhone(code);
        return ApiResult.result(flag);
    }

    @GetMapping("/getMchWxPhone/{code}")
    @OperationLog(name = "获取商户端微信授权手机号", type = OperationLogType.INFO)
    @ApiOperation(value = "获取商户端微信授权手机号", response = ApiResult.class)
    public ApiResult<Boolean> getMchWxPhone(@PathVariable String code) throws Exception {
        boolean flag = loginService.getMchWxPhone(code);
        return ApiResult.result(flag);
    }

    @GetMapping("/getAgentWxPhone/{code}")
    @OperationLog(name = "获取代理商端微信授权手机号", type = OperationLogType.INFO)
    @ApiOperation(value = "获取代理商端微信授权手机号", response = ApiResult.class)
    public ApiResult<Boolean> getAgentWxPhone(@PathVariable String code) throws Exception {
        boolean flag = loginService.getAgentWxPhone(code);
        return ApiResult.result(flag);
    }

    @PostMapping("/getAppCode")
    @OperationLog(name = "获取小程序码", type = OperationLogType.INFO)
    @ApiOperation(value = "获取小程序码", response = ApiResult.class)
    public ApiResult<String> getAppCode(@Validated @RequestBody GetAppCodeParam getAppCodeParam) throws Exception {
        String codeUrl = loginService.getAppCode(getAppCodeParam);
        return ApiResult.ok(codeUrl);
    }

    @GetMapping("/appCode/download/{code}")
    @ApiOperation(value = "下载小程序码")
    public void download(HttpServletResponse response, @PathVariable("code") String code) throws Exception {
        loginService.download(response, code);
    }


}
